#pragma once

namespace gtl
{

namespace dui
{

static const char* lua_class_lib =
"-- classlib.lua 2.03 \n\n\
\
-- PRIVATE \n\n\
\
--[[ \n\
	Define unique value for identifying ambiguous base objects and inherited \n\
		attributes. Ambiguous values are normally removed from classes and objects, \n\
		but if keep_ambiguous == true they are left there and the ambiguous value \n\
		is made to behave in a way useful for debugging. \n\
]]"

"local ambiguous = { __type = 'ambiguous' } \n"
"local remove_ambiguous \n\n"

"if keep_ambiguous then \n\n\
\
	-- Make ambiguous complain about everything except tostring() \n\
	local function invalid(operation) \n\
		return function() \n\
			error('Invalid ' .. operation .. ' on ambiguous') \n\
		end \n\
	end \n\
	local ambiguous_mt = \n\
	{ \n\
		__add		= invalid('addition'), \n\
		__sub		= invalid('substraction'), \n\
		__mul		= invalid('multiplication'), \n\
		__div		= invalid('division'), \n\
		__mod		= invalid('modulus operation'), \n\
		__pow		= invalid('exponentiation'), \n\
		__unm		= invalid('unary minus'), \n\
		__concat	= invalid('concatenation'), \n\
		__len		= invalid('length operation'), \n\
		__eq		= invalid('equality comparison'), \n\
		__lt		= invalid('less than'), \n\
		__le		= invalid('less or equal'), \n\
		__index		= invalid('indexing'), \n\
		__newindex	= invalid('new indexing'), \n\
		__call		= invalid('call'), \n\
		__tostring	= function() return 'ambiguous' end, \n\
		__tonumber	= invalid('conversion to number') \n\
	} \n\
	setmetatable(ambiguous, ambiguous_mt) \n\n\
\
	-- Don't remove ambiguous values from classes and objects \n\
	remove_ambiguous = function() end \n\n\
\
else \n\n\
\
	-- Remove ambiguous values from classes and objects \n\
	remove_ambiguous = function(t) \n\
		for k, v in pairs(t) do \n\
		if v == ambiguous then t[k] = nil end \n\
		end \n\
	end \n\n\
\
end \n\n"


"--[[ \n\
	Reserved attribute names. \n\
]]\n\n"

"local reserved = \n\
{ \n\
	__index			= true, \n\
	__newindex		= true, \n\
	__type			= true, \n\
	__class			= true, \n\
	__bases			= true, \n\
	__inherited		= true, \n\
	__from			= true, \n\
	__shared		= true, \n\
	__user_init		= true, \n\
	__name			= true, \n\
	__initialized	= true \n\
}\n\n"

"--[[ \n\
	Some special user-set attributes are renamed. \n\
]]\n\n"

"local rename = \n\
{ \n\
	__init	= '__user_init', \n\
	__set	= '__user_set', \n\
	__get	= '__user_get' \n\
}\n\n"

"--[[ \n\
	The metatable of all classes, containing: \n\n\
\
	To be used by the classes: \n\
	__call()		for creating instances \n\
	__init() 		default constructor \n\
	is_a()			for checking object and class types \n\
	implements()	for checking interface support \n\n\
\
	For internal use: \n\
	__newindex()	for controlling class population \n\
]]\n\n"

"local class_mt = {} \n"
"class_mt.__index = class_mt \n\n"

"--[[ \n\
	This controls class population. \n\
		Here 'self' is a class being populated by inheritance or by the user. \n\
]] \n\n"

"function class_mt:__newindex(name, value) \n\n\
\
	-- Rename special user-set attributes \n\
	if rename[name] then name = rename[name] end \n\n\
\
		-- __user_get() needs an __index() handler \n\
		if name == '__user_get' then \n\
			self.__index = value and function(obj, k) \n\
				local v = self[k] \n\
				if v == nil and not reserved[k] then v = value(obj, k) end \n\
					return v \n\
				end or self \n\n\
\
		-- __user_set() needs a __newindex() handler \n\
		elseif name == '__user_set' then \n\
			self.__newindex = value and function(obj, k, v) \n\
				if reserved[k] or not value(obj, k, v) then rawset(obj, k, v) end \n\
			end or nil \n\n\
\
	end \n\n\
\
	-- Assign the attribute \n\
	rawset(self, name, value) \n\
end \n\n"

"--[[ \n\
	This function creates an object of a certain class and calls itself \n\
		recursively to create one child object for each base class. Base objects \n\
		of unnamed base classes are accessed by using the base class as an index \n\
		into the object, base objects of named base classes are accessed as fields \n\
		of the object with the names of their respective base classes. \n\
		Classes derived in shared mode will create only a single base object. \n\
		Unambiguous grandchildren are inherited by the parent if they do not  \n\
		collide with direct children. \n\
]] \n\n"

"local function build(class, shared_objs, shared) \n\n\
\
	-- Repository for storing shared objects \n\
	shared_objs = shared_objs or {} \n\n\
\
	-- Shared inheritance creates a single shared child per base class \n\
	if shared and shared_objs[class] then return shared_objs[class] end \n\n\
\
	-- New object \n\
	local obj = { __type = 'object' } \n\n\
\
	-- Repository for storing inherited base objects \n\
	local inherited = {} \n\n\
\
	-- Build child objects for each base class \n\
	for i, base in ipairs(class.__bases) do \n\
		local child = build(base, shared_objs, class.__shared[base]) \n\
		obj[base.__name] = child \n\n\
\
		-- Get inherited grandchildren from this child \n\
		for c, grandchild in pairs(child) do \n\n\
\
			-- We can only accept one inherited grandchild of each class, \n\
			-- otherwise this is an ambiguous reference \n\
			if not inherited[c] then inherited[c] = grandchild \n\
			elseif inherited[c] ~= grandchild then inherited[c] = ambiguous \n\
			end \n\
		end \n\
	end \n\n\
\
	-- Accept inherited grandchildren if they don't collide with \n\
	-- direct children \n\
	for k, v in pairs(inherited) do \n\
		if not obj[k] then obj[k] = v end \n\
	end \n\n\
\
	-- Remove ambiguous inherited grandchildren \n\
	remove_ambiguous(obj) \n\n\
\
	-- Object is ready \n\
	setmetatable(obj, class) \n\n\
\
	-- If shared, add it to the repository of shared objects \n\
	if shared then shared_objs[class] = obj end \n\n\
\
	return obj \n\
end \n\n"

"--[[ \n\
	The __call() operator creates an instance of the class and initializes it. \n\
]] \n\n"

"function class_mt:__call(...) \n\
	local obj = build(self) \n\
	obj:__init(...) \n\
	return obj \n\
end \n\n"

"--[[ \n\
	The implements() method checks that an object or class supports the \n\
	interface of a target class. This means it can be passed as an argument to \n\
	any function that expects the target class. We consider only functions \n\
	and callable objects to be part of the interface of a class. \n\
]] \n\n"

"function class_mt:implements(class) \n\n\
\
	-- Auxiliary function to determine if something is callable \n\
	local function is_callable(v) \n\
		if v == ambiguous then return false end \n\
		if type(v) == 'function' then return true end \n\
		local mt = getmetatable(v) \n\
		return mt and type(mt.__call) == 'function' \n\
	end \n\n\
\
	-- Check we have all the target's callables (except reserved names) \n\
	for k, v in pairs(class) do \n\
		if not reserved[k] and is_callable(v) and not is_callable(self[k]) then \n\
			return false \n\
		end \n\
	end \n\
	return true \n\
end \n\n"

"--[[ \n\
	The is_a() method checks the type of an object or class starting from  \n\
	its class and following the derivation chain upwards looking for \n\
	the target class. If the target class is found, it checks that its \n\
	interface is supported (this may fail in multiple inheritance because \n\
	of ambiguities). \n\
]] \n\n"

"function class_mt:is_a(class) \n\n\
\
	-- If our class is the target class this is trivially true \n\
	if self.__class == class then return true end \n\n\
\
	-- Auxiliary function to determine if a target class is one of a list of \n\
	-- classes or one of their bases \n\
	local function find(target, classlist) \n\
		for i, class in ipairs(classlist) do \n\
			if class == target or find(target, class.__bases) then \n\
				return true \n\
			end \n\
		end \n\
		return false \n\
	end \n\n\
\
	-- Check that we derive from the target \n\
	if not find(class, self.__bases) then return false end \n\n\
\
	-- Check that we implement the target's interface. \n\
	return self:implements(class) \n\
end \n\n"

"--[[ \n\
	Factory-supplied constructor, calls the user-supplied constructor if any, \n\
	then calls the constructors of the bases to initialize those that were \n\
	not initialized before. Objects are initialized exactly once. \n\
]] \n\n"

"function class_mt:__init(...) \n\
	if self.__initialized then return end \n\
	if self.__user_init then self:__user_init(...) end \n\
	for i, base in ipairs(self.__bases) do \n\
		self[base.__name]:__init(...) \n\
	end \n\
	self.__initialized = true \n\
end \n\n"

"-- PUBLIC \n\n"

"--[[ \n\
	Utility type and interface checking functions \n\
]] \n\n"

"function typeof(value) \n\
	local t = type(value) \n\
	return t =='table' and value.__type or t  \n\
end \n\n"

"function classof(value) \n\
	local t = type(value) \n\
	return t == 'table' and value.__class or nil \n\
end \n\n"

"function classname(value) \n\
	if not classof(value) then return nil end \n\
	local name = value.__name \n\
	return type(name) == 'string' and name or nil \n\
end \n\n"

"function implements(value, class) \n\
	return classof(value) and value:implements(class) or false \n\
end \n\n"

"function is_a(value, class) \n\
	return classof(value) and value:is_a(class) or false \n\
end \n\n"

"--[[ \n\
	Use a table to control class creation and naming. \n\
]] \n\n"

"class = {} \n"
"local mt = {} \n"
"setmetatable(class, mt) \n\n"

"--[[ \n\
	Create a named or unnamed class by calling class([name, ] ...).  \n\
	Arguments are an optional string to set the class name and the classes or \n\
	shared classes to be derived from. \n\
]] \n\n"

"function mt:__call(...) \n\n\
\
	local arg = {...} \n\n\
\
	-- Create a new class \n\
	local c = \n\
	{ \n\
		__type = 'class', \n\
		__bases = {}, \n\
		__shared = {} \n\
	} \n\
	c.__class = c \n\
	c.__index = c \n\n\
\
	-- A first string argument sets the name of the class. \n\
	if type(arg[1]) == 'string' then \n\
		c.__name = arg[1] \n\
		table.remove(arg, 1) \n\
	else \n\
		c.__name = c \n\
	end \n\n\
\
	-- Repository of inherited attributes \n\
	local inherited = {} \n\
	local from = {} \n\n\
\
	-- Inherit from the base classes \n\
	for i, base in ipairs(arg) do \n\n\
\
		-- Get the base and whether it is inherited in shared mode \n\
		local basetype = typeof(base) \n\
		local shared = basetype == 'share' \n\
		assert(basetype == 'class' or shared,  \n\
				'Base ' .. i .. ' is not a class or shared class') \n\
		if shared then base = base.__class end \n\n\
\
		-- Just in case, check this base is not repeated \n\
		assert(c.__shared[base] == nil, 'Base ' .. i .. ' is duplicated') \n\n\
\
		-- Accept it \n\
		c.__bases[i] = base \n\
		c.__shared[base] = shared \n\n\
\
		-- Get attributes that could be inherited from this base \n\
		for k, v in pairs(base) do \n\n\
\
			-- Skip reserved and ambiguous attributes \n\
			if not reserved[k] and v ~= ambiguous and \n\
											inherited[k] ~= ambiguous then \n\n\
\
				-- Where does this attribute come from? \n\
				local new_from \n\n\
\
				-- Check if the attribute was inherited by the base \n\
				local base_inherited = base.__inherited[k] \n\
				if base_inherited then \n\n\
\
					-- If it has been redefined, cancel this inheritance  \n\
					if base_inherited ~= v then		-- (1) \n\
						base.__inherited[k] = nil \n\
						base.__from[k] = nil \n\n\
\
					-- It is still inherited, get it from the original \n\
					else \n\
						new_from = base.__from[k] \n\
					end \n\
				end \n\n\
\
				-- If it is not inherited by the base, it originates there \n\
				new_from = new_from or { class = base, shared = shared } \n\n\
\
				-- Accept a first-time inheritance \n\
				local current_from = from[k] \n\
				if not current_from then \n\
					from[k] = new_from \n\n\
\
					-- Wrap methods so that they are called with the correct \n\
					-- base object self. For functions that are not methods \n\
					-- this creates some useless code. \n\
					if type(v) == 'function' then \n\
						local origin = new_from.class \n\
						inherited[k] = function(self, ...) \n\
							return origin[k](self[origin.__name], ...) \n\
						end \n\n\
\
					-- Properties are copied \n\
					else \n\
						inherited[k] = v \n\
					end \n\n\
\
				-- Attributes inherited more than once are ambiguous unless \n\
				-- they originate in the same shared class. \n\
				elseif current_from.class ~= new_from.class or \n\
						not current_from.shared or not new_from.shared then \n\
					inherited[k] = ambiguous \n\
					from[k] = nil \n\
				end \n\
			end \n\
		end \n\
	end \n\n\
\
	-- Remove ambiguous inherited attributes \n\
	remove_ambiguous(inherited) \n\n\
\
	-- Set the metatable now, it monitors attribute setting and does some \n\
	-- special processing for some of them. \n\
	setmetatable(c, class_mt) \n\n\
\
	-- Set inherited attributes in the class, they may be redefined afterwards \n\
	for k, v in pairs(inherited) do c[k] = v end	-- checked at (1) \n\
	c.__inherited = inherited \n\
	c.__from = from \n\n\
\
	return c \n\
end \n\n"

"--[[ \n\
	Create a named class and assign it to a global variable of the same name. \n\
	Example: class.A(...) is equivalent to (global) A = class('A', ...). \n\
]] \n\n"

"function mt:__index(name) \n\
	return function(...) \n\
		local c = class(name, ...) \n\
		getfenv()[name] = c \n\
		return c \n\
	end \n\
end \n\n"

"--[[ \n\
	Wrap a class for shared derivation. \n\
]] \n\n"

"function shared(class) \n\
	assert(typeof(class) == 'class', 'Argument is not a class') \n\
	return { __type = 'share', __class = class } \n\
end \n"
;

} // end of namespace dui

} // end of namespace gtl